<?php
namespace ImiDemo\HttpDemo\MainServer\Controller;

use Imi\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
use ImiDemo\HttpDemo\MainServer\Model\TestRedisModel;
use Imi\Pool\PoolManager;

/**
 * RedisModel操作 演示
 * @Controller("/RedisModel/")
 * @View(renderType="json")
 */
class RedisModelController extends HttpController
{
    /**
     * RedisModel操作 演示
     * 访问：http://127.0.0.1:8080/RedisModel/test
     * 
     * @Action
     * @return void
     */
    public function test()
    {
        // 写入
        $model = TestRedisModel::newInstance();
        $model->setId(123);
        $model->setName('imi');
        $model->setAge(100);
        $model->save();

        // 读取-直接传key
        $model2 = TestRedisModel::find('123-imi');
        var_dump($model2->toArray());

        // 修改再保存
        $model2->setAge(200);
        $model2->save();

        // 读取-传参数组成
        $model3 = TestRedisModel::find([
            'id'    =>    '123',
            'name'    =>    'imi'
        ]);
        var_dump($model3->toArray());

        // 批量读取
        $list = TestRedisModel::select('kkk', '123-imi', [
            'id'    =>    '123',
            'name'    =>    'imi'
        ]);
        foreach($list as $item)
        {
            var_dump($item->toArray());
        }

        // 批量删除
        TestRedisModel::deleteBatch('kkk', '123-imi');

        return [
            'success'    =>    true,
        ];
    }

}